<template>
  <view class="wrap">
    <view class="item-box">
      <view class="left-title">文件标识</view>
      <view class="item-right">
        <u-input input-align="right" v-model="givenumber.fileCode" :type="type" :border="border" />
        <view style="color: red;">*</view>
      </view>
    </view>
    <u-line></u-line>
    <view class="item-box">
      <view class="left-title">父类字段</view>
      <view class="item-right">
        <u-input v-model="givenumber.parentName" type="select" input-align="right" :border="border"
          @click="showParentPicker()" />
        <view @click="addSub">
          <image style="margin-left: 5rpx; width: 40rpx;height: 40rpx;" src="../../../static/toolSet/add.png">
          </image>
        </view>
      </view>

    </view>

    <view class="sub-box">
      <block v-for="(item,i) in givenumber.subCodes">
        <view class="sub-item">
          <view class="tag">
            <u-input :type="type" v-model="givenumber.subCodes[i]" :border="border" />
          </view>
          <view class="del" @click="delsubCode(i)">
            删除
          </view>

        </view>
      </block>
    </view>
    <u-line></u-line>
    <view class="item-box">
      <view class="left-title">年份字段码</view>
      <view class="item-right">
        <u-input v-model="givenumber.yearName" type="select" input-align="right" :border="border"
          @click="showYearPicker()" />
      </view>
    </view>
    <u-line></u-line>
    <view class="item-box">
      <view class="left-title">文件名称</view>
      <view class="item-right">
        <u-input :type="type" v-model="givenumber.fileName" input-align="right" :border="border" />
      </view>
    </view>

    <view class="btn" @click="giveNumber()">生成文件编号</view>
    <u-picker mode="selector" v-model="yearShow" :range="years" range-key="dictLabel" @confirm="confirmYear">
    </u-picker>
    <u-picker mode="selector" v-model="parentShow" :range="parents" range-key="dictLabel" @confirm="confirmParent">
    </u-picker>


  </view>

</template>

<script>
  export default {
    data() {
      return {
        yearShow: false,
        yearCode: "",
        yearName: "",
        years: [],
        parentShow: false,
        parent: "",
        parents: [],

        givenumber: {
          fileCode: "",
          parentCode: "",
          parentName: "",
          subCodes: [],
          yearCode: "",
          fileName: "",
          giveNumber: ""
        }


      }
    },
    mounted() {


    },
    onLoad() {
		var login=this.checkLogin();
					if(!login)
					{
						return false;
					}

		
      this.getDictData();
    },
    methods: {

      showYearPicker() {
        this.yearShow = !this.yearShow;
      },
      showParentPicker() {
        this.parentShow = !this.parentShow;
      },
      navigateBack() {
        uni.navigateBack()
      },
      confirmParent(e) {
        this.givenumber.parentCode = this.parents[e].dictValue;
        this.givenumber.parentName = this.parents[e].dictLabel;
        this.parentShow = false;
      },
      confirmYear(e) {
        this.givenumber.yearCode = this.years[e].dictValue;
        this.givenumber.yearName = this.years[e].dictLabel;
        this.yearShow = false;
      },
      addSub() {
        this.givenumber.subCodes.push("");
        console.log(this.givenumber.subCodes.length);
      },
      delsubCode(i) {
        this.givenumber.subCodes.splice(i, 1)
      },
      giveNumber() {
        if (this.$u.test.isEmpty(this.givenumber.fileCode)) {

          uni.showToast({
            title: "文件标识必填!",
            icon: "none"
          })

          return;
        }
        if (this.$u.test.isEmpty(this.givenumber.parentCode)) {
          uni.showToast({
            title: "父类字段!",
            icon: "none"
          })
          return;
        }
        if (this.$u.test.isEmpty(this.givenumber.yearCode)) {
          uni.showToast({
            title: "年份字段码!",
            icon: "none"
          })
          return;
        }
        if (this.$u.test.isEmpty(this.givenumber.fileName)) {
          uni.showToast({
            title: "文件名称!",
            icon: "none"
          })
          return;
        }
        var that = this;
        this.$u.api.giveNumber(this.givenumber)
          .then(res => {
            that.givenumber.giveNumber = res.data;
            uni.navigateTo({
              url: '/subpackage/tool/givenumber/soonGiveNumber?data=' + encodeURIComponent(
                JSON
                .stringify(that.givenumber))
            });
          });
      },
      getDictData() {

        this.$u.api.getDictData({
            dictType: "sys_parent"
          })
          .then(res => {
            this.parents = res.rows;
            console.log(res);
          });

        this.$u.api.getDictData({
            dictType: "sys_year"
          })
          .then(res => {
            this.years = res.rows;
          });
      },


    }
  }
</script>

<style lang="scss">
  .wrap {
    margin-left: 20rpx;
    margin-right: 20rpx;
  }

  .sub-box {
    display: flex;
    flex-direction: column;

    .sub-item {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      margin-top: 5rpx;
      margin-bottom: 5rpx;
      margin-left: 30rpx;

      .del {
        color: #ff0000;
      }
    }
  }

  .item-box {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .item-right {
    display: flex;
    flex-direction: row;
    align-items: center;
  }

  .left-title {

    font-size: 28rpx;
    font-family: PingFang SC;

    color: #333333;
    line-height: 48px;
  }

  .right-value {
    font-size: 28rpx;
    font-family: PingFang SC;
    color: #808080;
    line-height: 70rpx;
  }



  .btn {
    margin-top: 100rpx;
    margin-left: 10%;
    margin-right: 10%;
    height: 80rpx;
    line-height: 80rpx;
    background: #3BA2FF;
    border-radius: 40px;
    text-align: center;

    font-size: 32rpx;
    font-family: PingFang SC;

    color: #FFFFFF;


  }
</style>
